#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <map>

#include "irrlicht.h"
#include "zone.h"
#include "bsp2zone.h"
#include "bsp_reader.h"

int main(int argc, char **argv) 
{
	try
	{
		std::string map = "../baseq3/maps/";
		std::string out;
		bsp2zone b2z;
		Bsp b;

		if (argc == 2)
			map += argv[1];
		else
			map += "way_test";
		
		map += ".bsp";

		out.assign(map.c_str(), 0, map.rfind(".bsp"));
		out += ".w2g";

		b.load(map);
		b2z.computeZones(b);
		b2z.computeZoneNeighbors();

		b2z.computeWayPointGraph();

		b2z.computeDijkstraAndSaveToFile(out);


		unsigned int i = 0;
		std::cout << "Graphe de zone: " << std::endl;
		graph::directed<zone, segment_t>& g = b2z.getZones();
		i = 0;
		while (i < g.vertexes().size())
		{
			std::cout << g.vertex(i).getName() << " {\n";

			std::list<int> sucksessors = g.successors(i);
			std::list<int>::iterator it = sucksessors.begin();
			while (it != sucksessors.end())
			{
				std::cout << "\t" << g.vertex(*it).getName() << " - " << g.edge(i, *it) << std::endl;
				it++;
			}
			std::cout << "}" << std::endl;
			i++;
		}

		std::cout << "Graphe des waypoints: " << std::endl;
		graph::directed<segment_t, float>& G = b2z.getWaypoints();
		i = 0;
		while (i < G.vertexes().size())
		{
			std::cout << i << ": " << G.vertex(i) << " {\n";

			std::list<int> sucksessors = G.successors(i);
			std::list<int>::iterator it = sucksessors.begin();
			while (it != sucksessors.end())
			{
				std::cout << "\t" << G.vertex(*it) << " \t- " << G.edge(i, *it) << std::endl;
				it++;
			}
			std::cout << "}" << std::endl;
			i++;
		}
	
	}
	catch(Exception e)
	{
		std::cout << "Something bad happened: " << e << std::endl;
	}

	return 0;
}

